<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Service\FlyCartService;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * 购物车
 * Class CartController
 * @package App\Controller
 */
class CartController extends AbstractController
{

    /**
     * @Inject
     * @var FlyCartService
     */
    private $cart;

    /**
     * @return int
     */
    public function get_user_id(): int
    {
        return (int)$this->request->getHeaderLine('admin_id') ?? 0;
    }

    /**
     * 添加购物车（添加、修改、选中）
     * @return ResponseInterface
     */
    public function addCart(): ResponseInterface
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '请先登录');
        }
        $params = $this->request->all();
        if (empty($params['carts'])) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未选择sku');
        }
        $cart_style = (int)$this->request->input('cart_style', FlyCartService::CART_STYLE_SHOP);
        $cart_id = $this->cart->cartAdd(['carts' => $params['carts'], 'user_id' => $user_id], $cart_style);
        if ($cart_style == FlyCartService::CART_STYLE_SHOP) {
            if (!$cart_id) {
                return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '添加失败');
            }
        }
        $shop_id = $this->request->input('shop_id', 0);
        $count = $this->cart->getCartSku($user_id, $cart_style, $shop_id);
        return $this->jsonResponse->success([
            'total_price' => $count['total_price'],
            'total_quantity' => $count['total_quantity'],
            'cart_id' => (is_numeric($cart_id) ? $cart_id : 0),
            'fails_cart' => (is_array($cart_id) ? $cart_id : [])], '添加成功');
    }

    /**
     * 删除购物车
     * @return ResponseInterface
     */
    public function delCart(): ResponseInterface
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录请先登录');
        }
        $cart_id = (array)$this->request->input('cart_id', []);
        $clear_cart = (int)$this->request->input('clear_cart', 0);
        $cart_style = (int)$this->request->input('cart_style', FlyCartService::CART_STYLE_SHOP);
        $shop_id = (int)$this->request->input('shop_id', 0);
        $res = $this->cart->delCart($user_id, $cart_id, $clear_cart, $cart_style);
        if (!$res) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '操作失败');
        }
        $count = $this->cart->getCartSku($user_id, $cart_style, $shop_id);
        return $this->jsonResponse->success(['total_price' => $count['total_price'], 'total_quantity' => $count['total_quantity']], '删除成功');
    }

    /**
     * 获取购物车列表
     * @return ResponseInterface
     */
    public function getCart(): ResponseInterface
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录请先登录');
        }
        $cart_style = $this->request->input('cart_style', FlyCartService::CART_STYLE_SHOP);
        $cart_style = $cart_style == FlyCartService::CART_STYLE_SHOP ? FlyCartService::CART_STYLE_SHOP : FlyCartService::CART_STYLE_ORDER;
        $shop_id = (int)$this->request->input('shop_id', 0);
        return $this->jsonResponse->success($this->cart->getCartSku($user_id, $cart_style, $shop_id), '获取购物车成功');
    }

    /**
     * 获取sku数量
     * @return ResponseInterface
     */
    public function getCartNum(): ResponseInterface
    {
        $user_id = $this->get_user_id();
        if (empty($user_id)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '未登录请先登录');
        }
        return $this->jsonResponse->success(['sku_count' => $this->cart->getCartNum($user_id)], '获取种类数成功');
    }

}